<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <p class="site-navbar__brand-lg pointer">
          管理后台系统登录
        </p>
        <a class="site-navbar__brand-mini" href="javascript:;">水表报装</a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix">
      <el-menu class="site-navbar__menu" mode="horizontal">
        <el-menu-item
          class="site-navbar__switch"
          index="0"
          @click="sidebarFold = !sidebarFold"
        >
          <icon-svg name="zhedie"></icon-svg>
        </el-menu-item>
      </el-menu>
      <el-menu
        class="site-navbar__menu site-navbar__menu--right"
        mode="horizontal"
      >
        <el-menu-item
          v-if="isAuth('info:newsdetails:list')"
          @click="$router.push({ path: '/system-message/index' })"
        >
          <template slot="title">
            <el-badge :value="msgCount">
              <icon-svg name="icon-tixing" class="el-icon-tixing"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>
        <el-menu-item class="site-navbar__avatar" index="3">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png" :alt="userName" />{{
                userName
              }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()"
                >修改密码</el-dropdown-item
              >
              <el-dropdown-item @click.native="handleUserInfo()"
                >账户信息</el-dropdown-item
              >
              <el-dropdown-item @click.native="logoutHandle()"
                >退出</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password
      v-if="updatePassowrdVisible"
      ref="updatePassowrd"
    ></update-password>
    <userInfoDialog ref="userInfoDialog" />
  </nav>
</template>

<script>
import UpdatePassword from "./main-navbar-update-password";
import userInfoDialog from "./userInfoDialog";
import { clearLoginInfo } from "@/utils";
import { mapState } from "vuex";
export default {
  data() {
    return {
      updatePassowrdVisible: false
    };
  },
  components: {
    UpdatePassword,
    userInfoDialog
  },
  computed: {
    ...mapState({
      msgCount: state => state.user.msgCount
    }),
    navbarLayoutType: {
      get() {
        return this.$store.state.common.navbarLayoutType;
      }
    },
    sidebarFold: {
      get() {
        return this.$store.state.common.sidebarFold;
      },
      set(val) {
        this.$store.commit("common/updateSidebarFold", val);
      }
    },
    mainTabs: {
      get() {
        return this.$store.state.common.mainTabs;
      },
      set(val) {
        this.$store.commit("common/updateMainTabs", val);
      }
    },
    userName: {
      get() {
        console.log(this.$store.state.user)
        return this.$store.state.user.name;
      }
    }
  },
  methods: {
    // userinfo
    handleUserInfo() {
      this.$refs.userInfoDialog.open({});
    },
    // 修改密码
    updatePasswordHandle() {
      this.updatePassowrdVisible = true;
      this.$nextTick(() => {
        this.$refs.updatePassowrd.init();
      });
    },
    // 退出
    logoutHandle() {
      this.$confirm(`确定进行[退出]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          this.$Api.http({ url: "/logout" }).finally(() => {
            clearLoginInfo();
            // 测试 关闭清除 tabs列表
            this.$store.commit("common/updateMainTabs", []);
          });
        })
        .catch(() => {});
    }
  }
};
</script>
<style lang="scss" scoped>
.el-icon-tixing {
  font-size: 30px !important;
}
</style>
